package edu.hrbu.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import edu.hrbu.mapper.UserMapper;
import edu.hrbu.pojo.User;
import edu.hrbu.service.UserService;
import edu.hrbu.util.SqlSessionUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class UserServiceImpl implements UserService {
    @Override
    public User checkMyName(Integer userId, String userName) {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = mapper.checkMyName(userId, userName);
        sqlSession.close();
        return user;
    }

    @Override
    public boolean editInfo(User user) {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        boolean f = mapper.editInfo(user);
        if (f) {
            sqlSession.commit();
        }
        sqlSession.close();
        return f;
    }

    @Override
    public List<User> getUserList(Integer roleId) {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<User> userList = mapper.getUserList(roleId, null, null);
        sqlSession.close();
        return userList;
    }

    @Override
    public User checName(String userName) {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = mapper.checkName(userName);
        sqlSession.close();
        return user;
    }

    @Override
    public boolean addUser(User user) {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        boolean f = mapper.addUser(user);
        if (f) {
            sqlSession.commit();
        }
        sqlSession.close();
        return f;
    }

    @Override
    public User getUserById(int userId, int roleId) {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = mapper.getUserById(userId, roleId);
        sqlSession.close();
        return user;
    }

    @Override
    public boolean delUser(int userId) {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        boolean f = mapper.delUser(userId);
        if (f) {
            sqlSession.commit();
        }
        sqlSession.close();
        return f;
    }

    @Override
    public PageInfo<User> getUserListPage(Integer pageNum, Integer myRoleId, String userName, Integer roleId) {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        PageHelper.startPage(pageNum, 3);
        List<User> userList = mapper.getUserList(myRoleId, userName, roleId);
        PageInfo<User> pageInfo = new PageInfo<>(userList);
        sqlSession.close();
        return pageInfo;
    }

    @Override
    public List<User> getUserListByRoleId(int roleId) {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<User> userList = mapper.getUserListByRoleId(roleId);
        sqlSession.close();
        return userList;
    }

    @Override
    public User checkLogin(String uname, String upwd) {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = mapper.checkUserLogin(uname, upwd);
        sqlSession.close();
        return user;
    }
}
